package com.zyzd.commontools.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by LZY on 2017/7/12.
 * <p>
 * 日期工具类，提供格式化日期，解析字符串日期为时间
 */

public class DateUtil {

    public static String format(long time, String formatStr) {
        if (TextUtils.isEmpty(formatStr)) return "";
        return format(new Date(time), formatStr);
    }

    public static String format(Date date, String formatStr) {
        if (date == null) return "";
        return new SimpleDateFormat(formatStr, Locale.getDefault()).format(date);
    }

    public static Date parseToDate(String time, String dateFormat) {
        if (TextUtils.isEmpty(time) || TextUtils.isEmpty(dateFormat)) return null;

        try {
            return new SimpleDateFormat(dateFormat, Locale.getDefault()).parse(time);
        } catch (ParseException e) {
            return null;
        }
    }

    public static long parseToStamp(String time, String dataFormat) {
        Date date = parseToDate(time, dataFormat);
        if (date == null) return 0;
        return date.getTime();
    }

}
